package com.itboye.mengwu.main;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.login.ui.LoginActivity;
import com.itboye.mengwu.app.ui.aiwujia.ActivityAiwujiaMomPublish;
import com.itboye.mengwu.app.ui.aiwujia.ActivityAiwujiaYuErjingPublish;
import com.itboye.mengwu.app.ui.aiwujia.ActivityBaoBaoLePublish;
import com.itboye.mengwu.app.ui.aiwujia.FragmentYuEr;
import com.itboye.mengwu.app.ui.aizhiwu.BaoBaoLeFragment;
import com.itboye.mengwu.testpic.ui.PublishedActivity;
import com.itboye.mengwu.widget.AiZhiWuPagerSlidingTabStrip;
import com.itboye.mengwu.widget.PagerSlidingTabStrip;

/***
 * 爱之屋Fragment
 * 
 * @author dsawe
 * 
 */
public class AizhiwuFragment extends BaseFragment {
	String[] titles = { "育儿经", "宝宝乐" };
	ViewPager pager;
	AiZhiWuPagerSlidingTabStrip tabs;
	TextView txt_more;
	int tag;
	String uid;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_aizhiwu;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public void onMyActivityCreated() {
		pager.setAdapter(new MyAdapter(getChildFragmentManager(), titles));
		tabs.setViewPager(pager);
		pager.addOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				if (arg0 == 0) {
					tag = 0;
				} else if (arg0 == 1) {
					tag = 1;
					
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		txt_more.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d("hu--tag2", tag + "");
				uid = SPUtils.get(getContext(), null, SpPublicParams.UID, "")
						+ "";
				Log.e("uid", uid);
				if ("".equals(uid) | uid == null) {
					Intent intent = new Intent(getActivity(),
							LoginActivity.class);
					startActivity(intent);
				} else {
					if (tag == 0) {
						Intent intent = new Intent(getActivity(),
								ActivityAiwujiaYuErjingPublish.class);
						startActivity(intent);
					} else if (tag == 1) {
						Intent intent = new Intent(getActivity(),
								PublishedActivity.class);
						startActivity(intent);
					}
				}
			}
		});

	}

	public class MyAdapter extends FragmentStatePagerAdapter {
		String[] _titles;

		public MyAdapter(FragmentManager fm, String[] titles) {
			super(fm);
			_titles = titles;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return _titles[position];
		}

		@Override
		public int getCount() {
			return titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			Fragment bfrag2 = null;
			if (position == 0) {
				bfrag2 = new FragmentYuEr();
			} else {
				bfrag2 = new BaoBaoLeFragment();
			}
			return bfrag2;
		}
	}
}
